
    let obj = {};
    setPage()
   



$('.container').click(function(e){


 if(e.target.getAttribute('name') === "-"){
    let num = $('.num').val()-0;
    $('.num').val(num === 1 ? 1 : num-1);
           
    

   
 }else if(e.target.getAttribute('name') === "+"){

         let num = $('.num').val()-0;

        console.log(num);

        $('.num').val(num === obj.goods_number-0 ? obj.goods_number : num+1);

 }else if(e.target.getAttribute('name') === "inCart"){

            const cookie = myGetCookie();
            console.log(cookie);
    if(cookie.login === undefined){

        if( window.confirm('您还没有登录,点击确定,跳转登录页面') ){

                    window.location.href = './login.html';
        }
    
    }else{
         if(localStorage.cart === undefined){

                    console.log(localStorage.cart);
                    console.log(obj);

                    obj.buy = true;

                    let number = $('.num').val()-0; 

                    if(number >= 1 && number <= obj.goods_number-0){

                    obj.num = number;

                    }else{
                    window.alert(`您输入的数量有误,请输入1至库存${obj.goods_number}的数量。`)
                    return ;
                     }

                      localStorage.cart = JSON.stringify( [obj] );

         }else{
                    // 购物车已经创建
                    const arr = JSON.parse(localStorage.cart);
                    console.log(arr);

                if(arr.length === 0){
                        // 如果是空车

                        // 当前商品添加购买状态属性 默认是购买
                            obj.buy = true
                            let number = $('.num').val()-0;

                            if(number >= 1 && number <= obj.goods_number-0){
                                obj.num = number;
                            }else{
                                window.alert(`您输入的数量有误,请输入1至库存${obj.goods_number}的数量。`)
                                return ;
                            }
                            localStorage.cart = JSON.stringify( [obj] );
                }else{

                        let bool = arr.some(function(item){
                            return item.goods_id === obj.goods_id; 
                        })

                    if(bool ){

                    for(let i = 0 ; i <= arr.length-1 ; i++){
                     if(arr[i].goods_id === obj.goods_id){

                         let number = $('.num').val()-0;

                         if(arr[i].num + number >= 1 && arr[i].num + number <= arr[i].goods_number-0){
                             arr[i].num = arr[i].num + number;

                             console.log(arr[i]);
                         }else{
                             
                             window.alert(`您购买的数量超出范围,您最多还能购买${arr[i].num}至${arr[i].goods_number}`);
                             return;
                         }
                     }
                    }

                 localStorage.cart = JSON.stringify( arr );
             }else{
                 // 商品如果不存在 重新写入数组里
                 obj.buy = true;

                 let number = $('.num').val()-0;
                 if(number >= 1 && number <= obj.goods_number-0){
                     obj.num = number;
                 }else{
                     window.alert(`您输入的数量有误,请输入1至库存${obj.goods_number}的数量。`)
                     return ;
                 }
                  // 将 新对象 写入 数组的起始位置
                 arr.unshift( obj );
                 localStorage.cart = JSON.stringify( arr );
             }

             
         }
     }
     window.location.href = './cart.html';
 }


}


   

})
 















async function setPage(){
    const valObj = myGetUrlValue();
    console.log(valObj);
    const res = await myPromiseAjax('../server/goods_detail.php', 'post' , {goods_id:valObj.goods_id}, 'json');



    obj = res;

    let str = 
    `
    <div class="box">
        <div class="top">
                商品详情信息
        </div>
        <div class="body">
            <div class="left">
                <img src="${res.goods_big_logo}">
            </div>
            <div class="right">
                <h4>${res.goods_name}</h4>
                <h1>￥ ${res.goods_price}</h1>

               
                <div class="btn-group">
                    <button type="button" class="btn">XL</button>
                    <button type="button" class="btn">L</button>
                    <button type="button" class="btn">M</button>
                    <button type="button" class="btn">S</button>
                    <button type="button" class="btn">XS</button>
                </div>

                <p class="plus">
                    <input value="1" class="num">

                   
                    <button name="-" type="button" class="jian">-</button>
                    <button name="+" type="button" class="jia">+</button>
                 
                </p>
                
                <p class="radio">
                    <a href="javascript:;" class="btn-lg" role="button">立即购买</a>
                    <a name="inCart" href="JavaScript:;" class="btn-lg" role="button">加入购物车</a>
                </p>

            </div>
        </div>

        <ul class="nav">
            <li class="active"><a href="#">商品详细信息</a></li>
            <li><a href="#">商品参数信息</a></li>
            <li><a href="#">相关商品</a></li>
        </ul>
        <div class="banner">
             ${res.goods_introduce}
        </div>
    </div>
    
    `;
    // 写入
    $('.container').html(str);





     // 给尺码按钮添加点击事件 改变样式
        $('.btn').click(function(){
            $(this).addClass('active').siblings().removeClass('active')
        })
            
           





       
  

  
 

} 



    












